package com.example.myapplication.Fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myapplication.activity.LoadingActivity;
import com.example.myapplication.R;
import com.example.myapplication.adapter.recyclerViewAdapter.TopicAdapter;
import com.example.myapplication.api.MusicApiService;
import com.example.myapplication.entity.HotTopic;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TopicFragment  extends Fragment {
    private RecyclerView recyclerview_topic;
    private View view;
    private TopicAdapter adapter;
    private List<HotTopic.HotBean> hotBeanList = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_topic,container,false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerview_topic = view.findViewById(R.id.recyclerview_topic);
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getContext());
        recyclerview_topic.setLayoutManager(manager);
        adapter = new TopicAdapter(getContext());
        recyclerview_topic.setAdapter(adapter);
        loadData();
    }

    private void loadData() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("https://autumnfish.cn/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        MusicApiService service =  retrofit.create(MusicApiService.class);

        Call<HotTopic> call = service.getHotTopic(LoadingActivity.cookie);
        call.enqueue(new Callback<HotTopic>() {
            @Override
            public void onResponse(Call<HotTopic> call, Response<HotTopic> response) {
                HotTopic baseResponse = response.body();
                hotBeanList = baseResponse.getHot();
                adapter.setData(hotBeanList);
            }

            @Override
            public void onFailure(Call<HotTopic> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}
